package com.fan.txl.ui;

import android.app.SearchManager;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.base.txl.utils.ComponentUtils;
import com.base.txl.utils.RxUtils;
import com.fan.txl.R;
import com.fan.txl.data.Friend;
import com.fan.txl.data.contact.Contact;
import com.fan.txl.ui.base.RxActivity;
import com.fan.txl.ui.routers.ComponentRouters;
import com.github.mzule.activityrouter.annotation.Router;


import java.util.ArrayList;
import java.util.List;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.observers.DisposableObserver;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
@Router({ComponentRouters.friend})
public class FriendUI extends RxActivity {
    FloatingActionButton fab;
    TextView textView2;
    private static final String[] PHONES_PROJECTION = new String[] {
            Phone.DISPLAY_NAME, Phone.NUMBER };

    /**联系人显示名称**/
    private static final int PHONES_DISPLAY_NAME_INDEX = 0;

    /**电话号码**/
    private static final int PHONES_NUMBER_INDEX = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);

        textView2=(TextView)findViewById(R.id.score);
         fab = (FloatingActionButton) findViewById(R.id.fab);
         fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Contact contact= new Contact("15108280907","张飞");
                ComponentUtils.start(view.getContext(),DesignUI.class,contact);
             }
        });

        handleIntent(getIntent());
        Observable.create(new ObservableOnSubscribe<List<Friend>>() {
            @Override
            public void subscribe(ObservableEmitter<List<Friend>> e) throws Exception {
                List<Friend> contacts = new ArrayList<Friend>();
                ContentResolver resolver = getContentResolver();
                // 获取手机联系人
                Cursor phoneCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);
                if (phoneCursor != null) {
                    while (phoneCursor.moveToNext()) {
                        //得到手机号码
                        String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
                        //当手机号码为空的或者为空字段 跳过当前循环
                        if (TextUtils.isEmpty(phoneNumber))
                            continue;
                        //得到联系人名称
                        String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);
                        Friend friends= new Friend();
                            friends.setName(contactName);
                            friends.setContact(phoneNumber);
                        contacts.add(friends);
                    }
                    phoneCursor.close();
                    e.onNext(contacts);
                }
            }
        })
                .compose(RxUtils.<List<Friend>>applySchedulers(RxUtils.IO_ON_UI_TRANSFORMER))
                .subscribe(new DisposableObserver<List<Friend>>() {

                    @Override
                    public void onNext(List<Friend> friends) {
                       // Toast.makeText(FriendUI.this, friends.get(0).getName(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleIntent(intent);
    }


    private void handleIntent(Intent intent){
        if (Intent.ACTION_SEARCH.equals(intent.getAction())){
            String query = intent.getStringExtra(SearchManager.QUERY);
            textView2.setText(query);
        }
    }

}
